package com.jokul.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jokul.connection.DataBase;
import com.jokul.model.Album;

/**
 *
 *@author jokul
 *@2013-4-8 
 *@下午8:50:10
 */
public class AlbumDaoImpl implements AlbumDao{

	@Override
	public List<Album> getAllAlbum(Integer userId) {
		List<Album> list = new ArrayList<Album>();
		Connection connection = null;
		PreparedStatement ps = null;
		String sql = "select album_id,album_name from album where user_id = ? ORDER BY create_time ASC";
		try {
			connection = DataBase.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setInt(1, userId);
			ResultSet rs = ps.executeQuery();
			while(rs.next()) {
				Album album = new Album();
				album.setAlbumId(rs.getInt("album_id"));
				album.setAlbumName(rs.getString("album_name"));
				list.add(album);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if(ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
		return list;
	}

}
